<template>
  <div>
    <el-button type="primary" @click="showAddChapterDialog">添加章节</el-button>
    <!-- 章节列表 TODO -->
    <ul class="chapterList">
      <li
        v-for="chapter in chapters"
        :key="chapter.id">
        <p>
          {{ chapter.title }}
          <span class="acts">
            <el-button type="text" @click="addVideo(chapter.id)">添加课时</el-button>
            <el-button type="text" @click="open(chapter.id)">编辑</el-button>
            <el-button type="text" @click="removeById(chapter.id)">删除</el-button>
          </span>
        </p>
        <!-- 视频 -->
        <ul class="chapterList videoList">
          <li
            v-for="video in chapter.videos"
            :key="video.id">
            <p>
              {{ video.title }}
              <el-tag v-if="!video.videoSourceId" size="mini" type="danger">
                {{ '尚未上传视频' }}
              </el-tag>
              <span class="acts">
                <el-tag v-if="video.free" size="mini" type="success">{{ '免费观看' }}</el-tag>
                <el-button type="text" @click="editVideo(chapter.id,video.id)">编辑</el-button>
                <el-button type="text" @click="removeVideoById(video.id)">删除</el-button>
              </span>
            </p>
          </li>
        </ul>
      </li>
    </ul>

    <div style="text-align:center">
      <el-button type="primary" @click="prev()">上一步</el-button>
      <el-button type="primary" @click="next()">下一步</el-button>
    </div>
    <chapter-form ref="chapterDialog"/>
    <!-- 课时表单对话框 -->
    <video-form ref="videoDialog"/>
  </div>
</template>
<script>
import ChapterForm from '@/views/course/components/chapter/ChapterForm'
import VideoForm from '@/views/course/components/chapter/VideoForm'
import chapterApi from '@/api/chapter'
import videoApi from '@/api/video'
export default {
  // 注册组件
  components: {
    ChapterForm,
    VideoForm
  },
  data() {
    return {
      flag: false,
      chapters: []
    }
  },
  created() {
    this.getChapters()
  },
  methods: {
    showAddChapterDialog() {
      // 弹出一个收集章的对话框表单
      this.$refs.chapterDialog.dialogFormVisible = true
    },
    prev() {
      this.$parent.active = 0
    },
    next() {
      this.$parent.active = 2
    },
    getChapters() {
      chapterApi.getChaptersByCourseId(this.$parent.courseId).then(res => {
        this.chapters = res.data.items
      })
    },
    // 弹出编辑框并回显信息
    open(id) {
      console.log(id)
      this.$refs.chapterDialog.dialogFormVisible = true
      chapterApi.getById(id).then(res => {
        this.$refs.chapterDialog.chapter = res.data.chapter
      })
    },
    // 根据id删除章节数据
    removeById(id) {
      this.$confirm('此操作将永久删除该【章节】,是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        return chapterApi.deleteById(id)
      }).then((response) => {
        this.getChapters()
        this.$message.success('删除成功')
      }).catch(error => {
        console.log('error', error)
        // 当取消时会进入catch语句:error = 'cancel'
        // 当后端服务抛出异常时：error = 'error'
        if (error === 'cancel') {
          this.$message.info('取消删除')
        }
      })
    },
    // 添加课程
    addVideo(chapterId) {
      // this.$refs.videoDialog.dialogVisible = true
      this.$refs.videoDialog.open(chapterId)
    },
    // 编辑课程
    editVideo(chapterId, videoId) {
      this.$refs.videoDialog.open(chapterId, videoId)
    },
    // 删除课时
    removeVideoById(videoId) {
      console.log(videoId)
      this.$confirm('此操作将永久删除该【课时】,是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        return videoApi.removeById(videoId)
      }).then((response) => {
        this.getChapters()
        this.$message.success('删除课时成功')
      }).catch(error => {
        console.log('error', error)
        // 当取消时会进入catch语句:error = 'cancel'
        // 当后端服务抛出异常时：error = 'error'
        if (error === 'cancel') {
          this.$message.info('取消删除')
        }
      })
    }

  }
}
</script>
<style>
.chapterList{
    position: relative;
    list-style: none;
    margin: 0;
    padding: 0;
}
.chapterList li{
  position: relative;
}
.chapterList p{
  float: left;
  font-size: 20px;
  margin: 10px 0;
  padding: 10px;
  height: 70px;
  line-height: 50px;
  width: 100%;
  border: 1px solid #DDD;
}
.chapterList .acts {
    float: right;
    font-size: 14px;
}

.videoList{
  padding-left: 50px;
}
.videoList p{
  float: left;
  font-size: 14px;
  margin: 10px 0;
  padding: 10px;
  height: 50px;
  line-height: 30px;
  width: 100%;
  border: 1px dashed #DDD;
}
</style>
